package reflect;

import java.lang.reflect.Field;

public class ReflectDemo8 {
    public static void main(String[] args) throws Exception {
        Class clas = Class.forName("reflect.Person");
        Object obj = clas.newInstance();

        Field field = clas.getDeclaredField("name");
        field.setAccessible(true);
        field.set(obj,"张三");
        String name = (String) field.get(obj);
        System.out.println("姓名："+name);
        field.setAccessible(false);

        Field field1 = clas.getDeclaredField("age");
        field1.setAccessible(true);
        field1.set(obj,5);
        int age = (int)field1.get(obj);
        System.out.println("年龄是："+age);
        field1.setAccessible(false);

        System.out.println(obj);
    }
}
